package com.king.cms.template;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException; 

import com.king.cms.action.BaseDispatchAction;
import com.king.cms.common.FileDoc;
import com.king.cms.common.FileRw;
import com.king.cms.common.FileUp;
import com.king.cms.common.InitSystem;
import com.king.cms.log.LogService;
import com.king.cms.util.page.JpegTool;
import com.king.cms.util.page.ZoomImage;
 

public class UploadAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(UploadAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws ServletException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try{
			
			String fileName = null; 
			FileUp fileUp = new FileUp(request);  
			List fileList = fileUp.getFileList();   
			String fileType = "0";
			String fileSaveName = null;
			if(null != fileList){
				FileRw f = new FileRw();
				for(int i = 0;i<fileList.size();i++){
					FileDoc file = (FileDoc)fileList.get(i);
					fileName = file.getFileName();
					fileType = fileUp.getFieldValue("fileType");
					fileSaveName = FileRw.radomFileName(file.getFileName()); 
					f.saveTempFile(file.getContent(), request.getRealPath("/")+FileRw.getFileSavePath(""),fileSaveName );
					f.saveTempFile(file.getContent(), request.getRealPath("/")+FileRw.getFileSavePath(""),fileName );
				   
				} 
			}  
	 			request.setAttribute("fileName", fileName);
	 			request.setAttribute("fileSaveName", fileSaveName);
	 			request.setAttribute("fileType", fileType);
				return mapping.findForward("templateupload");
			 
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	 
	}
}
